<template>
  <div class="search-selector">
    <div class="selector-line">
      <div class="selector-key">地区</div>
      <div class="selector-value">
        <span
          @click="onSelectRegion(null)"
          :class="{'selected': searchModel.provinceId === null}">
          全部
        </span>
        <span
          @click="onSelectRegion(region.id)"
          :class="{'selected': searchModel.provinceId === region.id}"
          :key="'region'+index"
          v-for="(region, index) in regionList">
          {{region.label}}
        </span>
      </div>
    </div>
    <div class="selector-line">
      <div class="selector-key">院校特色</div>
      <div class="selector-value">
        <span
          @click="onSelectFeature(null)"
          :class="{'selected': searchModel.propertyId === null}">
          全部
        </span>
        <span
          @click="onSelectFeature(feature.id)"
          :class="{'selected': searchModel.propertyId === feature.id}"
          :key="'feature'+index"
          v-for="(feature, index) in featureList">
          {{feature.codeName}}
        </span>
      </div>
    </div>
    <div class="selector-line">
      <div class="selector-key">专业大类</div>
      <div class="selector-value">
        <span
          @click="onSelectMajor(null)"
          :class="{'selected': majorModel.major === null}">
          全部
        </span>
        <span
          @click="onSelectMajor(major.id)"
          :class="{'selected': majorModel.major === major.id}"
          :key="'major'+index"
          v-for="(major, index) in majorList">
          {{major.codeName}}
        </span>
      </div>
    </div>
    <div class="selector-line">
      <div class="selector-key">专业小类</div>
      <div class="selector-value">
        <span
          @click="onSelectSubMajor(null)"
          :class="{'selected': majorModel.subMajor === null}">
          全部
        </span>
        <span
          @click="onSelectSubMajor(subMajor.id)"
          :key="'subMajor'+index"
          :class="{'selected': majorModel.subMajor === subMajor.id}"
          v-for="(subMajor, index) in subMajorList">
          {{subMajor.codeName}}
        </span>
      </div>
    </div>
    <div class="selector-line">
      <div class="selector-key">办学层次</div>
      <div class="selector-value">
         <span
           @click="onSelectLevel(null)"
           :class="{'selected': searchModel.schoolWay === null}">
          全部
        </span>
        <span
          @click="onSelectLevel(level.id)"
          :class="{'selected': searchModel.schoolWay === level.id}"
          :key="'level'+index"
          v-for="(level, index) in levelList">
          {{level.schoolWay}}
        </span>
      </div>
    </div>
    <div class="search">
      <div class="input-box">
        <input v-model="name" type="text" placeholder="请输入学校名称查询">
      </div>
      <div
        @click="doSearchName"
        class="search-btn">搜索
      </div>
    </div>
  </div>
</template>

<script>
import { simplifyArray } from '../../../utils/array.util'

export default {
  name: 'SearchSelector',
  data () {
    return {
      searchModel: {
        provinceId: null,
        propertyId: null,
        majorId: null,
        schoolWay: null,
      },
      name: '',
      majorModel: {
        major: null,
        subMajor: null,
      },
      regionList: [],
      featureList: [],
      majorList: [],
      levelList: [],
    }
  },
  mounted () {
    this.getSchoolFeature()
    this.getMajorList()
    this.getRegionList()
    this.getLevelList()
  },
  computed: {
    subMajorList () {
      return this.majorList
        .reduce((pre, cur) => [...pre, ...cur.childs], [])
        .filter(value => value.pid === this.majorModel.major)
    }
  },
  methods: {
    setPropertyId (propertyId) {
      this.searchModel.propertyId = propertyId
      this.$emit('doSearch', this.searchModel)
    },
    doSearchName () {
      this.$emit('doSearch', { name: this.name })
    },
    doSearch () {
      this.searchModel.majorId = this.majorModel.subMajor || this.majorModel.major
      this.$emit('doSearch', this.searchModel)
    },
    onSelectLevel (id) {
      this.searchModel.schoolWay = id
      this.$emit('doSearch', this.searchModel)
    },
    onSelectFeature (id) {
      this.setPropertyId(id)
    },
    onSelectRegion (id) {
      this.searchModel.provinceId = id
      this.$emit('doSearch', this.searchModel)
    },
    onSelectSubMajor (id) {
      this.majorModel.subMajor = id
      this.searchModel.majorId = id
      this.$emit('doSearch', this.searchModel)
    },
    onSelectMajor (id) {
      this.majorModel.major = id
      this.searchModel.majorId = id
      this.$emit('doSearch', this.searchModel)
    },
    getLevelList () {
      this.$http.get('/publicController/getSchoolWay').then(value => {
        this.levelList = value
      })
    },
    getRegionList () {
      this.$http.get('/publicController/getRegion').then(value => {
        this.regionList = simplifyArray(Object.values(value), (r) => {
          return {
            id: r.region,
            label: r.name,
          }
        }, 'childs')
      })
    },
    getMajorList () {
      this.$http.get('/publicController/getSchoolMajor').then(value => {
        // "id": 22,
        // "codeName": "哲学",
        // "shortName": null,
        // "codeValue": null,
        // "codeValueStr": "http://zyfiles.ubzyw.com/ubzy/file/RP3NcCYPtY.png",
        // "codeType": "major_type",
        // "showOrder": 1,
        // "status": 1,
        // "pid": 21,
        // "remark": null,
        // "childs": [
        this.majorList = value
      })
    },
    getSchoolFeature () {
      this.$http.get('/publicController/getSchoolProperty').then(value => {
        // "id": 17,
        // "codeName": "一流大学",
        // "shortName": null,
        // "codeValue": null,
        // "codeValueStr": null,
        // "codeType": "school_property",
        // "showOrder": 1,
        // "status": 1,
        // "pid": 16,
        // "remark": null,
        this.featureList = value
      })
    }
  }
}
</script>

<style scoped lang="less">
  .search-selector {
    background: #F5F6F7;
    box-sizing: border-box;
    padding: 20px;

    .selector-line {
      display: flex;
      line-height: 30px;
      border-bottom: 1px #E4E5E7 dashed;
      align-items: baseline;
      padding:10px 0
    }

    .selector-key {
      width: 80px;
      height: 28px;
      background: #9CA2A9;
      border-radius: 22px 0 12px 22px;
      text-align: center;
      font-size: 14px;
      color: #FFFFFF;
      line-height: 28px;
      margin-right: 24px;
    }

    .selector-value {
      flex: 1;
      overflow-y: auto;
      overflow-x: hidden;
      span:hover {
         color: #FF6B51;
      }

      .selected {
        color: #FF6B51;
      }

      span {
        word-break: keep-all;
        margin-right: 48px;
        color: #646A73;
        cursor: pointer;
      }
    }

    .search {
      .input-box {
        display: inline-block;
        width: 615px;
        height: 40px;
        margin-top: 20px;
        padding-left: 35px;
        background-image: url("/images/sh.png");
        background-size: 20px 20px;
        background-repeat: no-repeat;
        background-position: 10px 9px;

        input {
          width: 100%;
          &::-webkit-input-placeholder {
            color: #C3C6CB;
          }
        }
      }

      .search-btn {
        display: inline-block;
        width: 80px;
        height: 40px;
        margin-left: 16px;
        text-align: center;
        line-height: 40px;
        background: #FF6B51;
        border-radius: 4px;
        color: #FFFFFF;
        cursor: pointer;
        &:focus, &:hover {
          background-color: rgba(255, 107, 81, 0.77);
        }
      }
    }
  }
</style>
